<?php
$servername = 'localhost';
$username = 'username';
$password = 'password';
$dbname = 'testDB';

// 创建连接
$connect = mysqli_connect($servername, $username, $password);
 
// 检测连接
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error() . "<br />");
} else {
    echo "连接成功" . "<br />";
}

// 选择数据库
mysqli_select_db($connect, $dbname);
// 设置编码，防止中文乱码
mysqli_query($connect, "set names utf8");

// 创建数据表
$sql = "CREATE TABLE users(
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100) NOT NULL,
    password VARCHAR(40) NOT NULL,
    email VARCHAR(40) NOT NULL,
    phone VARCHAR(40) NOT NULL,
    collection VARCHAR(40) NOT NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
$retval = mysqli_query( $connect, $sql );
if(! $retval )
{
    die('数据表创建失败: ' . mysqli_error($connect) . "<br />");
}
echo "数据表创建成功" . "<br />";

// 预处理插入多条数据
$sqlinster = "INSERT INTO users (username, password, email, phone, collection)
    VALUES (?, ?, ?, ?, ?)";
// 为 mysqli_stmt_prepare() 初始化 statement 对象
$stmt = mysqli_stmt_init($connect);
//预处理语句
if (mysqli_stmt_prepare($stmt, $sqlinster)) {
    // 绑定参数
    mysqli_stmt_bind_param($stmt, 'sssss', $username, $password, $email, $phone, $collection);

    // 设置参数并执行
    $username = 'longaotian';
    $password = '123456';
    $email = 'longaotian@example.com';
    $phone = '123456678';
    $collection = 'longaotian';
    mysqli_stmt_execute($stmt);

    $username = 'zhaoritian';
    $password = '123456';
    $email = 'zhaoritian@example.com';
    $phone = '123456678';
    $collection = 'zhaoritian';
    mysqli_stmt_execute($stmt);

    $username = 'zhangsan';
    $password = '123456';
    $email = 'zhangsan@example.com';
    $phone = '123456678';
    $collection = 'zhangsan';
    mysqli_stmt_execute($stmt);

    $username = 'lisi';
    $password = '123456';
    $email = 'lisi@example.com';
    $phone = '123456678';
    $collection = 'lisi';
    mysqli_stmt_execute($stmt);

    $username = 'laowang';
    $password = '123456';
    $email = 'laowang@example.com';
    $phone = '123456678';
    $collection = 'laowang';
    mysqli_stmt_execute($stmt);

    echo '预处理插入完成' . '<br />';
}

mysqli_close($connect);
?>